package com.yb.xczx.rabbitmq.message.manager.config;

import com.yb.xczx.message.manager.api.XczxMessageManager;
import com.yb.xczx.message.manager.config.XczxMessageManagerAutoConfiguration;
import com.yb.xczx.rabbitmq.message.manager.annotation.ConditionalOnXczxMessageManagerEnabled;
import com.yb.xczx.rabbitmq.message.manager.api.XczxRabbitMQMessageManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/11 11:25
 * FileName: XczxRabbitmqMessageManagerAutoConfiguration
 * Description:
 */

@Configuration
@ConditionalOnXczxMessageManagerEnabled
@ConditionalOnProperty(value = "xczx.rabbitmq.message-manager.enabled",havingValue = "true",matchIfMissing = true)
@AutoConfigureAfter(XczxMessageManagerAutoConfiguration.class)
@ConditionalOnBean(XczxMessageManagerAutoConfiguration.class)
@Slf4j
public class XczxRabbitmqMessageManagerAutoConfiguration {

    @Bean
    public RabbitAdmin amqpAdmin(CachingConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }

    @Bean
    public RabbitTemplate rabbitTemplate(CachingConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jsonMessageConverter());

        return rabbitTemplate;
    }

    @Bean
    public Jackson2JsonMessageConverter jsonMessageConverter()
    {
        return new Jackson2JsonMessageConverter();
    }


    //设置接收方转换json
    @Bean
    public SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        //设置json序列化 消息转换器
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        return factory;
    }

    @Bean
    XczxMessageManager rabbitMQMessageManager(RabbitTemplate rabbitTemplate){
        return new XczxRabbitMQMessageManager(rabbitTemplate);
    }

}
